/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import org.apache.log4j.Logger;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;


/**
 *
 * This object conveys an ACL defined in acl_conf.xml file.
 * Methods of this object allow to get the ACL fields as
 * defined in the configuration, that the ACL plugin may or
 * may not take into account.
 *
 */
public class JBAcl implements Serializable
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBAcl.class);

    /** Description. */
    private String targetname;

    /** Modifiable attributes. */
    private List modifiableAttr = new ArrayList();

    /** "May visialize" parameter. */
    private String mayvisualize;

    /** "May visualize" flag. */
    private boolean canvisualize;

    /** "May create" parameter. */
    private String maycreate;

    /** "May create" flag. */
    private boolean cancreate;

    /** "May delete" parameter. */
    private String maydelete;

    /** "May delete" flag. */
    private boolean candelete;

    /** "May modify" parameter. */
    private String maymodify;

    /** "May modify" flag. */
    private boolean canmodify;

    /** Plug-in parameters. */
    private Properties param = new Properties();

    /**
     * Adds a modifiable attribute.
     *
     * @param value attribute
     */
    public void addModAttr(String value)
    {
        if (value != null)
        {
            // make the attribute case INSENSITIVE
            modifiableAttr.add(value.trim().toLowerCase());
        }
    }

    /**
     * Gets the "May create" parameter.
     * @return "May create" parameter.
     */
    public String getMaycreate()
    {
        return maycreate;
    }

    /**
     * Gets the "May delete" parameter.
     * @return "May delete" parameter.
     */
    public String getMaydelete()
    {
        return maydelete;
    }

    /**
     * Gets the "May modify" parameter.
     * @return "May modify" parameter.
     */
    public String getMaymodify()
    {
        return maymodify;
    }

    /**
     * Gets the "May visualize" parameter.
     * @return "May visualize" parameter.
     */
    public String getMayvisualize()
    {
        return mayvisualize;
    }

    /**
     * Sets the "May create" parameter.
     * @param amaycreate The "May create" parameter to set
     */
    public void setMaycreate(String amaycreate)
    {
        this.maycreate = amaycreate;
        cancreate = Boolean.valueOf(maycreate).booleanValue();
    }

    /**
     * Sets the "May delete" parameter.
     * @param amaydelete The "May delete" parameter to set
     */
    public void setMaydelete(String amaydelete)
    {
        this.maydelete = amaydelete;
        candelete = Boolean.valueOf(maydelete).booleanValue();
    }

    /**
     * Sets the "May modify" parameter.
     * @param amaymodify The "May modify" parameter to set
     */
    public void setMaymodify(String amaymodify)
    {
        this.maymodify = amaymodify;
        canmodify = Boolean.valueOf(maymodify).booleanValue();
    }

    /**
     * Sets the "May visualize" parameter.
     * @param amayvisualize The "May visualize" parameter to set
     */
    public void setMayvisualize(String amayvisualize)
    {
        this.mayvisualize = amayvisualize;
        canvisualize = Boolean.valueOf(mayvisualize).booleanValue();
    }

    /**
     * Gets the "May visualize" flag.
     *
     * @return the "May visualize" flag.
     */
    public boolean canVisualize()
    {
        return canvisualize;
    }

    /**
     * Gets the "May create" flag.
     *
     * @return "May create" flag.
     */
    public boolean canCreate()
    {
        return cancreate;
    }

    /**
     * Gets the "May delete" flag.
     *
     * @return "May delete" flag.
     */
    public boolean canDelete()
    {
        return candelete;
    }

    /**
     * Returns true if the "May modify" flag is true
     * and if the attribute is in the list of modifiable attributes.
     *
     * @param attr requested attribute
     *
     * @return true if the attribute is modifiable, else false
     */
    public boolean canModify(String attr)
    {
        if (attr == null)
        {
            return false;
        }

        if (canmodify)
        {
            // can modify some attributes
            // the key is case INSENSITIVE
            return modifiableAttr.contains(attr.trim().toLowerCase());
        }
        else
        {
            // not a single right to modify
            return false;
        }
    }

    /**
     * Gets list of modifiable attributes.
     * @return List of modifiable attributes.
     */
    public List getModifiableAttr()
    {
        return modifiableAttr;
    }

    /**
     * Gets ACL parameters.
     * @return ACL parameters.
     */
    public Properties getParam()
    {
        return param;
    }

    /**
     * Gets target name field.
     * @return target name field.
     */
    public String getTargetname()
    {
        return targetname;
    }

    /**
     * Sets target name field.
     * @param atargetname target name field.
     */
    public void setTargetname(String atargetname)
    {
        this.targetname = atargetname;
    }

    /**
     * Adds a plugin parameter.
     *
     * @param key parameter name
     * @param value parameter value
     */
    public void addParam(String key, String value)
    {
        param.setProperty(key, value);
    }

    /**
     * Displays the ACL.
     *
     * @return string formatted ACL
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("targetname = ");
        buf.append(targetname);
        buf.append(System.getProperty("line.separator"));
        buf.append("mayvisualize = ");
        buf.append(mayvisualize);
        buf.append(System.getProperty("line.separator"));
        buf.append("maycreate = ");
        buf.append(maycreate);
        buf.append(System.getProperty("line.separator"));
        buf.append("maydelete = ");
        buf.append(maydelete);
        buf.append(System.getProperty("line.separator"));
        buf.append("maymodify = ");
        buf.append(maymodify);
        buf.append(System.getProperty("line.separator"));

        if (modifiableAttr != null)
        {
            buf.append("List components of modifiableAttr = ");
            buf.append(System.getProperty("line.separator"));
            it = modifiableAttr.iterator();

            while (it.hasNext())
            {
                buf.append("...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (param != null)
        {
            buf.append("Properties components of param = ");
            buf.append(System.getProperty("line.separator"));

            Enumeration enum1 = param.propertyNames();
            String key;

            while (enum1.hasMoreElements())
            {
                buf.append("...");
                key = (String) enum1.nextElement();
                buf.append(key);
                buf.append(" = ");
                buf.append(param.getProperty(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method
}
